/**
 * 
 */
package com.zhiren.bookfood.dao;

import java.util.List;
import java.util.Properties;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
 * @author MARCO.PAN
 *
 */
public abstract class BaseDAO<T> extends HibernateDaoSupport {
	public static final String BASE_CLASS_PATH = "com.zhiren.bookfood.dao.entity.";
	protected Class<T> currentClass; //T.class
	
    public BaseDAO() {      
        Type genType = this.getClass().getGenericSuperclass();
        if(genType instanceof ParameterizedType)
        {
            Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
            currentClass = (Class<T>)params[0];
        }
    }
	
    protected T get(long id){
    	Long idL = new Long(id);
    	Integer idi = idL.intValue();
    	return (T)this.getHibernateTemplate().get(currentClass, idi);
    }
    
	protected void insert(T t){
		this.getHibernateTemplate().save(t);
	}
	
	protected List<T> findByProps(Properties props) {
		StringBuilder hql = new StringBuilder("From " + currentClass.getName() + " t WHERE 1 =1 ");
		for(Object key : props.keySet()){
			hql.append(" and t." + key + " " + props.getProperty((String)key));
		}
		return (List<T>)this.getHibernateTemplate().find(hql.toString());
	}
	
	protected void update(T t){
		this.getHibernateTemplate().update(t);
	}
	
}
